package com.形为型模式.中介者模式;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * 抽象中介者
 */
interface Medium{
    /**
     * 客户注册
     * @param member
     */
    void register(Customer member);

    /**
     * 转发
     * @param from
     * @param ad
     */
    void relay(String from,String ad);


}

/**
 * 具体中介者 房地产
 */
class EstateMedium implements Medium{
    List<Customer> members = new ArrayList<>();
    @Override
    public void register(Customer member) {
        if(!members.contains(member)) {
            members.add(member);
            member.setMedium(this);
        }
    }

    @Override
    public void relay(String from, String ad) {
        for(Customer ob:members){
            String name = ob.getName();
            if(!name.equals(from)){
                ((Customer)ob).receive(from,ad);
            }
        }
    }
}

/**
 * 抽象同事类 客户
 */
 abstract   class   Customer extends JFrame implements ActionListener {
     private static final long seriaVersionUID = -9009076556L;
     Medium medium;
     String name;
    JTextField SentText;
     JTextArea ReceiveArea;
    Customer(String name){
        super(name);
        this.name = name;
    }



    public void setMedium(Medium medium) {
        this.medium = medium;
    }
    public String getName(){
        return this.name;
    }
    abstract   void send(String ad);

    abstract void receive(String from,String ad);
    void ClientWindow(int x,int y ){
        Container cp;
        JScrollPane sp;
        JPanel p1,p2;
        cp=this.getContentPane();
        SentText=new JTextField(18);
        ReceiveArea=new JTextArea(10,18);
        ReceiveArea.setEditable(false);
        p1=new JPanel();
        p1.setBorder(BorderFactory.createTitledBorder("接收内容："));
        p1.add(ReceiveArea);
        sp=new JScrollPane(p1);
        cp.add(sp,BorderLayout.NORTH);
        p2=new JPanel();
        p2.setBorder(BorderFactory.createTitledBorder("发送内容："));
        p2.add(SentText);
        cp.add(p2,BorderLayout.SOUTH);
        SentText.addActionListener(this);
        this.setLocation(x,y);
        this.setSize(250, 330);
        this.setResizable(false); //窗口大小不可调整
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        String tempInfo = SentText.getText().trim();
        SentText.setText("");
        this.send(tempInfo);
    }

}

/**
 * 具体客户 卖方
 */
class Seller extends Customer{
    public Seller(String name) {
        super(name);
        ClientWindow(50,100);
    }

    @Override
    void send(String ad) {
        ReceiveArea.append(String.format("我（卖方）说 ：%s \n",ad));

        //使滚动条滚动到最底端
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
        medium.relay(name,ad);
    }

    @Override
    void receive(String from, String ad) {
        ReceiveArea.append(String.format("%s 说：%s \n",from,ad));
        //使滚动条滚动到最低端
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length());


    }
}

/**
 * 具体客户 买方
 */

class Buyer extends Customer{
    private static final long serialVersionUID = -474879276076308825L;
    public Buyer(String name) {
        super(name);
        ClientWindow(350,100);
    }

    @Override
    void send(String ad) {
        ReceiveArea.append("我(买方)说: "+ad+"\n");
        //使滚动条滚动到最底端
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
        medium.relay(name,ad);
    }

    @Override
    void receive(String from, String ad) {
        ReceiveArea.append(from +"说: "+ad+"\n");
        //使滚动条滚动到最底端
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
    }
}

/**
 * 中介者模式之 韶关房产
 */
public class MediumHouse {
    public static void main(String[] args) {
        Medium medium = new EstateMedium();
        Customer m1,m2;
        m1 = new Seller("张三-卖方");
        m2 = new Buyer("李四 买方");
        medium.register(m1);
        medium.register(m2);
    }
}
